package it.puzzler.gdx.game;

import it.puzzler.gdx.ui.puzzle.Element;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;

public class GdxPuzzler implements ApplicationListener {

	private static final String TAG = "GdxPuzzler";

	private Stage stage = null;
	private final Button button = null;

	private Texture texture = null;

	private Element element = null;

	private SpriteBatch batch;
	private Sprite sprite;

	private TextureRegion region;

	@Override
	public void create() {
		// initWithSprites();
		// initWithStage();
		initWithSpritesAndMask();
	}

	private void initWithSpritesAndMask() {

		batch = new SpriteBatch(100);

		texture = new Texture(Gdx.files.internal("puzzle.png"));
		region = new TextureRegion(texture, 0, 0, texture.getWidth(), texture.getHeight());
		sprite = new Sprite(texture, 0, 0, texture.getWidth(), texture.getHeight());
		sprite.setPosition(800, 10);
		sprite.setColor(0, 0, 1, 1);
	}

	private void initWithSprites() {
		batch = new SpriteBatch(100);

		texture = new Texture(Gdx.files.internal("puzzle.png"));

		sprite = new Sprite(texture, 0, 0, texture.getWidth(), texture.getHeight());
		sprite.setPosition(0, 0);
		sprite.setRotation(45);
		sprite.setScale(1f);
	}

	private void initWithStage() {
		stage = new Stage(0, 0, true);
		Gdx.input.setInputProcessor(stage);

		texture = new Texture(Gdx.files.internal("puzzle.png"));

		element = new Element();
		element.initTextureRegion(texture, 0, 0, texture.getWidth(), texture.getHeight());
		element.setX(0);
		element.setY(0);

		stage.addActor(element);
	}

	@Override
	public void dispose() {
		if (stage != null) {
			stage.dispose();
		}
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render() {
		// renderWithSprite();
		// renderWithStage();
		renderWithSpriteAndWithMask();
	}

	private void renderWithSpriteAndWithMask() {
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		Gdx.gl.glClearColor(0.5f, 0.5f, 1, 0.5f);

		batch.begin();

		batch.enableBlending();

		batch.setColor(1, 1, 0, 1f);
		batch.draw(texture, 10, 10);

		batch.setColor(0, 1, 0, 1f);
		batch.draw(region, 450, 10);

		sprite.draw(batch);

		batch.end();
	}

	private void renderWithSprite() {
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // This cryptic line clears
													// the screen.
		Gdx.gl.glClearColor(0, 1, 1, 0.5f);
		batch.begin();
		// Drawing goes here!

		// batch.enableBlending();

		sprite.setRotation(sprite.getRotation() + Gdx.graphics.getDeltaTime() * 60);
		sprite.draw(batch);

		// batch.draw(texture, 10, 10);
		batch.end();
	}

	private void renderWithStage() {
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		Gdx.gl.glClearColor(0.5f, 0, 0.5f, 0.5f);

		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
	}

	@Override
	public void resize(int width, int height) {
		if (stage != null) {
			stage.setViewport(width, height, false);
		}
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

}
